package queue;

import javax.transaction.xa.Xid;
import java.util.Arrays;

/**
 * @Description TODO
 * @Author zrj
 * @Date 2020/8/1 15:55
 * @Version 1.0
 */
public class LoopQueueTest implements Queue {

    int[] data;
    int front;
    int tail;
    int size;

    public LoopQueueTest(int capaCity) {
        data = new int[capaCity];
        front = 0;
        tail = 0;
        size = 0;
    }

    public LoopQueueTest() {
        this(5);
    }

    public int getCapacity() {
        return data.length - 1;
    }

    public void enqueue(int e) {

        if ((tail + 1) % data.length == front)
            resize(getCapacity() * 2);
        data[tail] = e;
        tail = (tail + 1) % data.length;
        size++;

    }

    private void resize(int newCapacity) {

        int[] newData = new int[newCapacity];
        for (int i = 0; i < size; i++) {
            newData[i] = data[(i + front) % data.length];
        }
        data = newData;
        front = 0;
        tail = size;
    }

    public int dequeue() {

        int ret = data[front];
        size--;
        front++;
        if (size == getCapacity() / 4 && getCapacity() / 2 != 0) {
            resize(getCapacity() / 2);
        }
        return ret;
    }

    public int getFront() {
        return data[0];
    }

    public int getSize() {
        return size;
    }

    public boolean isEmpty() {
        return size == 0;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("LoopQueueTest{" +
                "data=" + Arrays.toString(data) +
                ", front=" + front +
                ", length=" + data.length +
                ", tail=" + tail +
                ", size=" + size +
                '}');

        for (int i = front; i != tail; i = (i + 1)%data.length) {
            sb.append(" " + data[i]);
            if (i != size - 1) {
                sb.append(",");
            }
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        LoopQueueTest queueTest = new LoopQueueTest();
        for (int i = 0; i < 10; i++) {
            queueTest.enqueue(i);

        }
        System.out.println(queueTest.toString());
        System.out.println("----------------------");
        queueTest.dequeue();
        System.out.println(queueTest.toString());
        queueTest.dequeue();
        System.out.println(queueTest.toString());
        queueTest.enqueue(11);
        System.out.println(queueTest.toString());
        queueTest.dequeue();
        System.out.println(queueTest.toString());queueTest.dequeue();
        System.out.println(queueTest.toString());queueTest.dequeue();
        System.out.println(queueTest.toString());queueTest.dequeue();
        System.out.println(queueTest.toString());queueTest.dequeue();
        System.out.println(queueTest.toString());queueTest.dequeue();
        System.out.println(queueTest.toString());
        System.out.println("----------------------");
        queueTest.enqueue(11);
        System.out.println(queueTest.toString());queueTest.enqueue(11);
        System.out.println(queueTest.toString());queueTest.enqueue(11);
        System.out.println(queueTest.toString());
    }
}
